//
//  BAHomeViewModel.swift
//  BAweibo
//
//  Created by baggio on 2017/8/18.
//  Copyright © 2017年 baggio. All rights reserved.
//


import UIKit

//处理单条微博的业务逻辑
//ViewModel 不必要继承 NSObject
class BAHomeViewModel {
    
    var homeModel: BAHomeModel? {
        didSet{
            mbrankImage = dealMbrankLevel(mbrank: homeModel?.user?.mbrank)
            verifiedImage = dealVerified(verified: homeModel?.user?.verified)
            source = dealSouceStr(source: homeModel?.source)
        }
    }
    
    //会员等级
    var mbrankImage: UIImage?
    //时间，计算属性，需要每次滑动 cell 的时候都要计算一次，所以不能写在 didSet 中
    var createTimeStr: String? {
        return dealCreateTimeStr(created_at: homeModel?.created_at)
    }
    //来源
    var source: String?
    //会员认证
    var verifiedImage: UIImage?
    
    
    
    /*
     微博时间业务逻辑：
        如果是今年
            如果是今天
                如果小于60，显示：刚刚
                如果大于等于60 && 小于3600，显示：XX分钟前
                如果大于等于3600，显示：XX小时前
            如果是昨天，20170803 02:02:02，显示：昨天 02:02
            其他，20170707 07:07:07，显示：07月07日 07:07
        如果不是今年，20151212 12:12:12 显示格式：2015年12月12日 12:12
     */
    private func dealCreateTimeStr(created_at: Date?) -> String? {
        guard let createAt = created_at else {
            return "未知时间"
        }
        
        //凑时间测试
//        createAt = createAt.addingTimeInterval(-6000*1000*10)
        
        //格式化
        let df = DateFormatter()
        
        if isThisYear(createAt: createAt) {
            //今年
            let calendar = Calendar.current
            if calendar.isDateInToday(createAt) {
                //今天
                //计算秒数，时间-当前时间
                let s = -createAt.timeIntervalSinceNow
                if s < 60 {
                    return "刚刚"
                } else if s >= 60 && s < 3600 {
                    return "\(Int(s/60))分钟前"
                } else {
                    return "\(Int(s/3600))小时前"
                }
            } else if calendar.isDateInYesterday(createAt) {
                //昨天
                df.dateFormat = "昨天 HH:mm"
            } else {
                //其他
                df.dateFormat = "MM月dd日 HH:mm"
            }
        } else {
            //不是今年，注意大小写
            df.dateFormat = "yyyy年MM月dd日 HH:mm"
            
        }
        
        return df.string(from: createAt)
    }
    
    private func isThisYear(createAt: Date) -> Bool {
        //格式
        let df = DateFormatter()
        df.dateFormat = "yyyy"
        
        //微博时间
        let createAtStr = df.string(from: createAt)
        //当前时间
        let nowStr = df.string(from: Date())
        
        return createAtStr == nowStr
    }


    /*
     来源
        <a href="http://app.weibo.com/t/feed/5B6hUc" rel="nofollow">iPhone 6s Plus</a>
        <a href="http://app.weibo.com/t/feed/6vtZb0" rel="nofollow">微博 weibo.com</a>
        <a href="http://app.weibo.com/t/feed/1tqBja" rel="nofollow">360安全浏览器</a>
     */
    private func dealSouceStr(source: String?) -> String {
        guard let s = homeModel?.source, s.contains("\">") && s.contains("</") else {
            return "未知来源"
        }
        
        //获取光标位置
        let startIndex = s.range(of: "\">")
        let endIndex = s.range(of: "</")
        
        return "来自：\(s.substring(with: (startIndex?.upperBound)!..<(endIndex?.lowerBound)!))"
    }
    
    //会员等级 1-6
    private func dealMbrankLevel(mbrank: Int?) -> UIImage? {
        //print("处理会员等级业务逻辑")
        guard let m = mbrank else {
            return UIImage(named: "common_icon_membership")
        }
        if m >= 1 && m <= 6 {
            return UIImage(named: "common_icon_membership_level\(m)")
        } else {
            return UIImage(named: "common_icon_membership")
        }
    }
    
    /*
     会员认证 
        -1：没有认证；
        1，认证用户；
        2，3，5：企业认证；
        220：达人认证
     */
    private func dealVerified(verified: Int?) -> UIImage? {
        //print("处理会员认证业务逻辑")
        guard let v = verified else {
            return UIImage(named: "avatar_vgirl")
        }
        
        switch v {
//        case -1:
//            return UIImage(named: "avatar_vgirl")
        case 1:
            return UIImage(named: "avatar_vip")
        case 2,3,5:
            return UIImage(named: "avatar_enterprise_vip")
        case 220:
            return UIImage(named: "avatar_grassroot")
        default:
            return UIImage(named: "avatar_vgirl")
        }
    }

}
